package com.ruoyi.web.controller.wms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsItemLocation;
import com.ruoyi.wms.service.IWmsItemLocationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * 商品库位关系Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@Api(tags = "商品库位关系管理")
@RestController
@RequestMapping("/wms/itemLocation")
public class WmsItemLocationController extends BaseController
{
    @Autowired
    private IWmsItemLocationService wmsItemLocationService;

    /**
     * 查询商品库位关系列表
     */
    @ApiOperation(value = "获取商品库位关系列表", notes = "获取商品库位关系列表，支持分页和条件过滤")
    @PreAuthorize("@ss.hasPermi('wms:itemLocation:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsItemLocation wmsItemLocation)
    {
        startPage();
        List<WmsItemLocation> list = wmsItemLocationService.selectWmsItemLocationList(wmsItemLocation);
        return getDataTable(list);
    }

    /**
     * 导出商品库位关系列表
     */
    @PreAuthorize("@ss.hasPermi('wms:itemLocation:export')")
    @Log(title = "商品库位关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsItemLocation wmsItemLocation)
    {
        List<WmsItemLocation> list = wmsItemLocationService.selectWmsItemLocationList(wmsItemLocation);
        ExcelUtil<WmsItemLocation> util = new ExcelUtil<WmsItemLocation>(WmsItemLocation.class);
        util.exportExcel(response, list, "商品库位关系数据");
    }

    /**
     * 获取商品库位关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:itemLocation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsItemLocationService.selectWmsItemLocationById(id));
    }

    /**
     * 新增商品库位关系
     */
    @PreAuthorize("@ss.hasPermi('wms:itemLocation:add')")
    @Log(title = "商品库位关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsItemLocation wmsItemLocation)
    {
        wmsItemLocation.setCreateBy(getUsername());
        return toAjax(wmsItemLocationService.insertWmsItemLocation(wmsItemLocation));
    }

    /**
     * 修改商品库位关系
     */
    @PreAuthorize("@ss.hasPermi('wms:itemLocation:edit')")
    @Log(title = "商品库位关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsItemLocation wmsItemLocation)
    {
        wmsItemLocation.setUpdateBy(getUsername());
        return toAjax(wmsItemLocationService.updateWmsItemLocation(wmsItemLocation));
    }

    /**
     * 删除商品库位关系
     */
    @PreAuthorize("@ss.hasPermi('wms:itemLocation:remove')")
    @Log(title = "商品库位关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsItemLocationService.deleteWmsItemLocationByIds(ids));
    }

    /**
     * 根据物品ID获取推荐库位
     */
    @ApiOperation(value = "获取物品推荐库位列表", notes = "根据物品ID和仓库ID获取推荐库位列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "itemId", value = "物品ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "warehouseId", value = "仓库ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    })
    @PreAuthorize("@ss.hasPermi('wms:itemLocation:query')")
    @GetMapping("/recommended/{itemId}/{warehouseId}")
    public AjaxResult getRecommendedLocations(@PathVariable("itemId") Long itemId, @PathVariable("warehouseId") Long warehouseId)
    {
        List<WmsItemLocation> list = wmsItemLocationService.selectRecommendedLocationsByItemId(itemId, warehouseId);
        return success(list);
    }

    /**
     * 根据物品ID查询库位关系
     */
    @ApiOperation(value = "根据物品ID查询库位关系", notes = "根据物品ID查询所有相关的库位关系")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "itemId", value = "物品ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "warehouseId", value = "仓库ID", required = false, dataType = "long", paramType = "query", dataTypeClass = Long.class)
    })
    @PreAuthorize("@ss.hasPermi('wms:itemLocation:query')")
    @GetMapping("/item/{itemId}")
    public AjaxResult getByItemId(@PathVariable("itemId") Long itemId, @RequestParam(value = "warehouseId", required = false) Long warehouseId)
    {
        List<WmsItemLocation> list = wmsItemLocationService.selectWmsItemLocationByItemId(itemId, warehouseId);
        return success(list);
    }

    /**
     * 根据库位ID查询物品关系
     */
    @ApiOperation(value = "根据库位ID查询物品关系", notes = "根据库位ID查询所有相关的物品关系")
    @ApiImplicitParam(name = "locationId", value = "库位ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('wms:itemLocation:query')")
    @GetMapping("/location/{locationId}")
    public AjaxResult getByLocationId(@PathVariable("locationId") Long locationId)
    {
        List<WmsItemLocation> list = wmsItemLocationService.selectWmsItemLocationByLocationId(locationId);
        return success(list);
    }
}